package boardgamer.yavalath.client;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.MotionEvent;
import boardgamer.common.client.AbstractView;
import boardgamer.common.client.HistoryStack;
import boardgamer.common.client.animation.Screen;
import boardgamer.yavalath.client.animation.YavalathBackground;
import boardgamer.yavalath.client.lobby.LocalLobbyScreen;
import boardgamer.yavalath.client.mainmenu.MainMenuScreen;
import boardgamer.yavalath.client.threads.GameThread;
import boardgamer.yavalath.common.YavalathGameServer;
import boardgamer.yavalath.common.YavalathPlayerState;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;

import javax.annotation.Nullable;
import java.util.Map;

import static boardgamer.yavalath.common.YavalathPlayerState.PlayerMode.AI;

public class MainView extends AbstractView<YavalathActivity> {

  // thread
  private GameThread gameThread;

  // debugging stuff
  private long gameTick = 0;

  private HistoryStack historyStack;
  private MainMenuScreen mainMenuScreen;
  private LocalLobbyScreen localLobbyScreen;
  @Nullable private YavalathBackground background;
  
  @Nullable private YavalathGameServer server;
  @Nullable private YavalathReplaySaver replaySaver;

  public MainView(YavalathActivity activity) {
    super(activity);

    setActivity(activity);
  }

  @Deprecated
  private void setActivity(YavalathActivity activity) {
    this.mainMenuScreen = new MainMenuScreen(this);
    this.localLobbyScreen = new LocalLobbyScreen(this, activity);
    this.historyStack = new HistoryStack();
    this.historyStack.setCurrentScreen(mainMenuScreen);
  }

  public void startNewGame(final Map<YavalathPlayer, YavalathPlayerState> players) {
    gameThread.startAiThreads(Iterables.filter(players.keySet(), new Predicate<YavalathPlayer>() {
      @Override
      public boolean apply(@Nullable YavalathPlayer yavalathPlayer) {
        return players.get(yavalathPlayer).getMode() == AI;
      }
    }), server);
    
    Navigator.instance().startGame(players);
  }

  @Deprecated
  public void setGameThread(GameThread gameThread) {
    this.gameThread = gameThread;
  }

  public void showLocalGameLobby() {
    historyStack.setCurrentScreen(localLobbyScreen);
  }

  public void showScreen(Screen screen) {
    historyStack.setCurrentScreen(screen);
  }

  @Override
  public void handleUpdate(int elapsedTime) {
    if (background != null) {
      background.update(elapsedTime);
    }
    historyStack.getCurrentScreen().onUpdate(elapsedTime);
  }

  @Override
  protected void handleLayout(Rect canvasRect) {
    // Initialize the layouts
    mainMenuScreen.initialize(canvasRect);
    localLobbyScreen.initialize(canvasRect);
    SharedObjects.instance().getProFeatures().initializeScreens(this, activity);

    background = new YavalathBackground(canvasRect);
  }

  @Override
  protected void handleTouchEvent(MotionEvent event) {
    historyStack.getCurrentScreen().onTouchEvent(event);
  }

  public void back() {
    if (!historyStack.isHistoryEmpty()) {
      historyStack.back();
    }
  }

  @Override
  public void handleDraw(Canvas canvas) {
    if (background != null) {
      background.draw(canvas);
    }

    historyStack.getCurrentScreen().onDraw(canvas);

    //debugging text -------------------------------------------------------------
//    long diff = (System.currentTimeMillis() - gameTick);
//    gameTick = System.currentTimeMillis();
//    diff = (long) 1000.0 / diff;
//
//    canvas.drawText("FrameRate : " + diff, 50, 400, ImageHandler.normalPaint);
    //----------------------------------------------------------------------------
  }

  public Screen getCurrentScreen() {
    return historyStack.getCurrentScreen();
  }
}
